package com.meichis.yndms.http.RetrofitUtil;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.meichis.mcsappframework.http.CallException;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.HttpException;

public class RxSchedulersHelper {
    public static <T> ObservableTransformer<T, T> rxSchedulerHelper() {//compose处理线程
        return new ObservableTransformer<T, T>() {

            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream
                        .onErrorResumeNext(new Function<Throwable, ObservableSource<? extends T>>() {
                            @Override
                            public ObservableSource<? extends T> apply(Throwable throwable) throws Exception {
                                try {
                                    if (throwable instanceof HttpException) {
                                        HttpException httpException = (HttpException) throwable;
                                        if (httpException.code() == 502) {
                                            return Observable.error(CallException.handleException(new Throwable("网关错误（502）")));
                                        }
                                        ResponseBody responseBody = ((HttpException) throwable).response().errorBody();
                                        JsonObject jsonObject = new JsonParser().parse(responseBody.string()).getAsJsonObject();
                                        if (jsonObject.get("error") != null) {
                                            return Observable.error(CallException.handleException(new Throwable(jsonObject.get("error").getAsString() + "→" + jsonObject.get("error_description").getAsString())));
                                        } else if (jsonObject.get("error_description") != null) {
                                            return Observable.error(CallException.handleException(new Throwable(jsonObject.get("error_description").getAsString())));
                                        } else {
                                            return Observable.error(CallException.handleException(new Throwable(jsonObject.toString())));
                                        }
                                    }
                                } catch (Exception e) {
                                    return Observable.error(CallException.handleException(e));
                                }
                                return Observable.error(CallException.handleException(throwable));
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }
}
